﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XW.Utils;
using System.Web;
using Microsoft.AspNetCore.Http;

namespace XW.Data
{
    public class ConditionBase
    {
        public ConditionBase()
        {
            this.ConditionDic = new Dictionary<string, string>();
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        public int PageRows { get; set; }

        /// <summary>
        /// 第几页
        /// </summary>
        public int PageIndex { get; set; }

        public string SortField { get; set; }
        public string SortOrder { get; set; }

        public Dictionary<string, string> ConditionDic { get; set; }

        public string this[string key]
        {
            get
            {
                if (this.ConditionDic.ContainsKey(key))
                {
                    return this.ConditionDic[key];
                }
                return "";
            }
            set { AddOrUpdateCondition(key, value); }
        }

        public int GetInt(string key)
        {
            return CommonHelper.ToInt(this[key]);
        }
        public List<T> GetList<T>(string key)
        {
            var value = this[key];

            return value.Split(',').Select(i => i.To<T>()).ToList();
        }
        /// <summary>
        /// 从Http请求中或者添加
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static ConditionBase LoadConditionByRequest(HttpRequest request, int defaultPagesize = 20)
        {
            ConditionBase confition = new ConditionBase();
            confition.PageRows = CommonHelper.ToInt(request.Form["rows"], defaultPagesize);
            confition.PageIndex = CommonHelper.ToInt(request.Form["page"]);
            confition.SortField = request.Form["sort"];
            confition.SortOrder = request.Form["order"];
            foreach (var key in request.Query.Keys)
            {
                confition.AddOrUpdateCondition(key, request.Query[key]);
            }
            foreach (var key in request.Form.Keys)
            {
                confition.AddOrUpdateCondition(key, request.Form[key]);
            }
            return confition;
        }
        ///// <summary>
        ///// 从Http请求中或者添加
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //public static ConditionBase LoadConditionByRequest(HttpRequest request, int defaultPagesize = 20)
        //{
        //    ConditionBase confition = new ConditionBase();
        //    confition.PageRows = CommonHelper.ToInt(request.Form["rows"], defaultPagesize);
        //    confition.PageIndex = CommonHelper.ToInt(request.Form["page"]);
        //    confition.SortField = request.Form["sort"];
        //    confition.SortOrder = request.Form["order"];
        //    foreach (var key in request.Query.Keys)
        //    {
        //        confition.AddOrUpdateCondition(key, request.Query[key]);
        //    }
        //    foreach (var key in request.Form.Keys)
        //    {
        //        confition.AddOrUpdateCondition(key, request.Form[key]);
        //    }
        //    return confition;
        //}
        /// <summary>
        /// 添加或者更新筛选字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void AddOrUpdateCondition(string key, string value)
        {
            if (this.ConditionDic.ContainsKey(key))
            {
                this.ConditionDic[key] = value;
            }
            else
            {
                this.ConditionDic.Add(key, value);
            }

        }

        public string GetOrder(string defaultvalue = "")
        {
            if (SortField.IsNotNullAndEmpty())
            {
                return SortField + " " + SortOrder;
            }
            return defaultvalue;
        }

        public string GetSort4Mongo(string defaultSort)
        {

            if (SortField.IsNullOrEmpty())
            {
                return defaultSort;
            }
            if (SortOrder.IsNullOrEmpty())
            {
                SortOrder = "asc";
            }
            return SortField + ":" + (SortOrder == "asc" ? "1" : "-1");
        }
    }
}
